{extend name="public:layout" /}
{block name="title"}{$moudleName}列表{/block}
{block name="head"}
<script src="/public/plugins/tableExport.jquery.plugin/tableExport.min.js"></script>
	<style>
		.leave_layer p {height: 40px;line-height: 40px;}
	</style>
{/block}
{block name="body"}
<div class="wrapper wrapper-content">
    <div class="ibox">
        <div class="ibox-title">
            <ol class="breadcrumb">
                <ol class="breadcrumb">
                    <li class="active"><i class="fa fa-hand-o-right"></i> {$moudleName}列表</li>
                </ol>
            </ol>
            <span class="pull-right">
                <span class="label" style="background:#caebfb">已完成</span>
                <span class="label" style="background:#c9e7c9">正在进行</span>
                <span class="label" style="background:#ffe8d6">即将开始</span>
                <span class="label"><span class="fa fa-exclamation-triangle" style="color:#ff0000;"></span>异常</span>
            </span>
            <div class="clearfix"></div>
        </div>
        <div class="ibox-content">
            <div class="toolbar">
                <form class="form-inline" id="searchForm" method="post">
                    <div class="form-group">
                        <div class="input-group">
                            <select name="car_status" class="form-control" onchange="refresh();">
                                <option value="-1">全部订单</option>
                                <option value="0">待确认订单</option>
                                <option value="1">正在出车订单</option>
                                <option value="2">已收车订单</option>
                                <option value="3">已结算订单</option>
                                <option value="4">欠款订单</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <input type="text" name="btype_id" id="select-btype_id" placeholder="请选择客户" data-init="" style="width:100%;" />
                        <script>
                            In('selectpage',function(){
                                var dom=$('#select-btype_id');
                                dom.selectPage({
                                    keyField:'aid',
                                    showField:'title',
                                    multiple:false,
                                    pagination:true,
                                    dbTable:'car_btype',
                                    data:'/plus/ajax/selectpage',
                                    andOr:'OR',
                                    params : function(){return {'searchField':'company'}},
                                    eAjaxSuccess : function(data){
                                        if(data.totalRow>0){
                                            return data;
                                        }else{
                                            return false;
                                        }
                                    }
                                });
                            })
                        </script>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <input class="form-control" type="text" placeholder="请输入车牌号" name="car_sn" />
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <input class="form-control" type="text" placeholder="请输入司机姓名" name="fleet_driver" />
                        </div>
                    </div>
                    <div class="form-group">
                        <input class="form-control" name="rangdate" id="rangdate" type="text" value="{$rangdate}" placeholder="日期范围">
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <input class="form-control" type="text" id="order_start_adress" placeholder="请输入起点" name="order_start_adress" />
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <input class="form-control" type="text" id="order_end_adress" placeholder="请输入终点" name="order_end_adress" />
                        </div>
                    </div>
                    <button type="button" class="btn btn-info" onclick="refresh()"><i class="fa fa-search"></i> 搜索</button>
                    <a class="btn btn-white" href="{:url('index',['mid'=>$Request.param.mid,'tpl'=>'static_order'])}">
                        <i class="fa fa-trash"></i> 清空
                    </a>
                </form>
            </div>
            <div id="buttons">
                <button type="button" onclick="add();" class="btn btn-primary">
                    <i class="fa fa-sort"></i> 添加{$moudleName}
                </button>
                <span class="text-danger" id="statics"> </span>
                <!--
                <button type="button" onclick="upOrder();" class="btn btn-info">
                    <i class="fa fa-sort"></i> 排序
                </button>
                <button type="button" onclick="batchDel(CheckboxValue())" class="btn btn-danger">
                    <i class="fa fa-trash"></i> 删除
                </button>
                -->

            </div>
            <div class="table-responsive">
            <table class="table table-bordered" id="grid"></table>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="script"}
{literal}
<script type="text/html" id="leave_layer">
	{{ each rows}}
	<div class="leave_layer" style="padding: 10px 20px;"> 
		<p>姓名：{{ $value.username }}</p>
		<P>状态：请假</P>
		<P>请假开始时间：{{ $value.leave_start_time }}</P>
		<P>请假结束时间：{{ $value.leave_end_time }}</P>
		<P>请假原因：{{ $value.title }}</P>
		<P>审批情况:{{if $value.leave_status}}审核通过{{else}}审核中{{/if}}
        </P>
	</div>
	{{ /each }}
</script>
{/literal}
<script type="text/javascript">
    var is_mobile="{:is_mobile()}";
    var cardView=false;
    var group_id = "{$group_id}";
    if(is_mobile==1){
        cardView=true;
    }
    $(function () {
        //列定义
        var columnsData=[
            {field: 'state', checkbox:true,},
            {field: 'order_client', title: '客户',width:60,sortable:true},
            {field: 'car_sn', title: '车辆',formatter:function (value,row,index) {
                    var html='';
                    if(row.btype_aid==1){
                        html='<span class="label label-success" style="display:block;font-size:14px;padding:10px 8px;margin:5px;font-weight:500;">'+row.car_sn+'</span>';
                    }else{
                        html='<span class="label label-warning" style="display:block;font-size:14px;padding:10px 8px;margin:5px;font-weight:500;">'+row.car_sn+'</span>';
                    }
                    return html;
                }},
            {field: 'title', title: '行程单',
                formatter:function (value,row,index) {
                    var text=row.order_start_adress+'-'+row.order_end_adress;
                    if(row.order_delay>0){
                        text+=' <span class="label label-danger">延期'+row.order_delay+'天</span>';
                    }
                    if(row.short_price>0){
                        text+='<br>插单信息:['+row.short_info.order_start_adress+']-['+row.short_info.order_end_adress+']'
                    }
                    if(group_id==1 && row.car_status<3){
                        text+='<br/><a href="/system/op/edit.html?aid='+row.aid+'&mid='+row.mid+'">修改行程单信息</a>'
                    }else{
                        if(row.btype_aid==1 && row.car_status==0){
                            text+='<br/><a href="/system/op/edit.html?aid='+row.aid+'&mid='+row.mid+'">修改行程单信息</a>'
                        }
                    }
                    return text;
                }
            },
            {field: 'order_advance', title: '定金',width:60},
            {field: 'order_charge', title: '订单金额',sortable:true,
                formatter:function (value,row,index) {
                    if(row.short_price>0){
                        return '订单金额'+value+'元<br/>插单金额'+row.short_price;
                    }
                    return value+'元';
                }
            },
            {field: 'order_advance', title: '预付款',sortable:true},
            {field: 'order_pay', title: '已付款',sortable:true},
            {field: 'order_q', title: '欠款金额',sortable:true,
                formatter:function (value,row,index) {
                    var html = '';
                    if(row.car_status==3){ //已结算
                        if(row.discount_money>0){
                            html += '<p class="text-success">优惠'+row.discount_money+'元</p>';
                        }
                        if(value>0){
                            html+= '<p class="text-danger">欠款'+value+'元</p>';
                        }else{
                            html+= '<p class="text-success">已结清</p>';
                        }
                    }else{
                        html+= '<p class="text-warning">未结算</p>';
                    }
                    return html;
                }
            },
            {field: 'order_start_time', title: '出发时间',sortable:true,
                formatter:function (value,row,index) {
                    return value.substring(0,16);
                }
            },
            {field: 'order_end_time', title: '结束时间',sortable:true,
                formatter:function (value,row,index) {
                    return value.substring(0,16);
                }
            },
            {field: 'order_confirm', title: '确认信息',
                formatter:function (value,row,index) {
                    if(value!=''){
                        return '已确认';
                    }else{
                        return '未确认';
                    }
                }
            },
            {field: 'fleet_driver', title: '驾驶员',formatter:function (value,row,index) {
                    var html=row.fleet_driver;
                    if(row.fleet_driver_ids=='' || row.no_dri_ids.length==0){
                        return html;
                    }
                    if(row.no_dri_ids!=0){
                        var no_dri_ids=row.no_dri_ids;
                        var fleet_driver_ids = row.fleet_driver_ids.split(',');
                        for(var i=0;i<fleet_driver_ids.length;i++){
                            var index=no_dri_ids.indexOf(parseInt(fleet_driver_ids[i]));
                            if(index>-1){ //存在异常
                                html+='<a href="javascript:;" onclick="showLeave('+no_dri_ids.join(',')+');"> <span class="fa fa-exclamation-triangle" style="color:#ff0000;"></span> </a> ';
                            }
                        }
                    }
                    //未出车才可以清除司机
                    if(row.car_status==0){
                        html+='<a href="javascript:;" onclick="delLeave('+row.aid+');"> <span class="fa fa-times" style="color:#23c6c8;"></span> </a> ';
                    }
                    return html;
                   
            }},
            {field: 'operate', title: '操作', align: 'center', width:160,
                formatter:function (value,row,index) {
                    var msg='更换司机';
                    if(row.fleet_driver==''){
                        msg='指派司机';
                    }
                    var html='';
                    //html+='<a href="__URL__/edit.html?aid='+row.aid+'&mid='+row.mid+'" class="btn btn-primary btn-xs">编辑</a> ';
                    if(row.car_status==0){ //未出车可删除派车单
                        html+='<a href="javascript:;" data-aid="'+row.aid+'" onclick="selectDriver(this);" class="btn btn-info btn-xs">'+msg+'</a> ';
                        html+='<a href="javascript:;" onclick="setstatus(\''+row.aid+'\',\'0\');" class="btn btn-info btn-xs">出车</a> <br/>';
                        html+='<a href="javascript:;" onclick="setSjPremoney(\''+row.aid+'\',\''+row.sj_pre_money+'\');" class="btn btn-success btn-xs">预借款</a> ';
                        html+='<a href="javascript:;" onclick="doAction(this,refresh);"' +
                            'data-msg="确认删除吗？" data-url="__URL__/del.html" data-params="aid='+row.aid+'&mid='+row.mid+'" class="btn btn-danger btn-xs">删除</a>  <br/>';
                    }
                    if(row.car_status==1){
                        html+='<a href="javascript:;" onclick="setstatus(\''+row.aid+'\',\'1\');" class="btn btn-info btn-xs">收车</a>  <br/>';
                    }
                    if(row.car_status==2){ //收车或者管理员才可以填写结算单
                        html+='<a href="__URL__/edit.html?aid='+row.aid+'&mid='+row.mid+'&tpl=edit_account" class="btn btn-success btn-xs">结算</a>  <br/>';
                    }
                    if(group_id==1 && row.car_status==3){ //管理员退回结算单
                        html+='<a href="javascript:;" onclick="doAction(this,refresh);"' +
                            'data-msg="退回后需要财务重新核算相关费用<br/>确认退回结算吗？" data-url="/system/logic.order/backstatus.html" data-params="aid='+row.aid+'&mid='+row.mid+'" class="btn btn-danger btn-xs">退回结算</a>  <br/>';
                    }
                    html+='<a href="__URL__/edit.html?aid='+row.aid+'&mid='+row.mid+'&tpl=car_send" class="btn btn-warning btn-xs">打印派车单</a>  <br/>';
                    return html;
                }
            }
        ];
        In('btable', function () {
            $('#grid').bootstrapTable({
                showExport:true,
                exportTypes:['csv', 'txt', 'excel'],
                showColumns:true,
                url: '__URL__/pagelist.html?mid={$Request.param.mid}',
                cardView:cardView,
                toolbar:'#buttons',
                sortName:'aid',
                sortOrder:'desc',
                method:'post',
                pagination:true,
                queryParamsType:'page',
                sidePagination:'server',
                pageSize:60, //每页记录数
                showRefresh:true, //显示刷新按钮
                showToggle:true, //显示切换视图按钮
                showPaginationSwitch:true, //显示数据条数选择框
                idField:'aid', //主键列
                clickToSelect:true, //设置true 将在点击行时，自动选择rediobox 和 checkbox
                singleSelect:false, //设置true 将禁止多选
                maintainSelected:true, //设置为true 将记住选择项
                queryParams:function (params) {
                    params.op="{$Request.param.op}";
                    params.car_sn=$("input[name='car_sn']").val();
                    params.car_status=$("select[name='car_status']").val();
                    params.fleet_driver=$("input[name='fleet_driver']").val();
                    params.rangdate=$("#rangdate").val();
                    params.order_start_adress=$("#order_start_adress").val();
                    params.order_end_adress=$("#order_end_adress").val();
                    params.btype_id = $('#select-btype_id').val();
                    return params;
                },
                columns: columnsData,
                rowStyle:function rowStyle(row, index) {
                    if(row.car_status==0){ //出车中
                        return { css: { "background": "#ffe8d6","color":"#333" } };
                    }
                    if(row.car_status==1){ //出车中
                        return { css: { "background": "#c9e7c9","color":"#333" } };
                    }
                    if(row.car_status==2){ //已完成
                        return { css: { "background": "#caebfb","color":"#333" } };
                    }
                    return { };
                },
                onLoadSuccess:function (data) {
                    var statics=data.statics;
                    var html='';
                   if(statics==null){
                     	return "";
                    }
                    if(statics.total_order_count>0){
                        html+=" 行程单数量："+statics.total_order_count+'笔';
                    }
                    if(statics.total_order_charge>0){
                        html+=" 订单总金额："+statics.total_order_charge+'元';
                    }
                    if(statics.calc_total_order_pay>0){
                        html+=" 已付款金额："+statics.calc_total_order_pay+'元';
                    }
                    if(statics.total_order_advance>0){
                        html+=" 欠款金额："+statics.calc_total_q+'元';
                    }
                    $('#statics').html(html);
                },
                detailView:true, //显示详情
                detailFormatter:function (index, row) {
                    var html='<p class="text-danger">录入人:'+row.action_name;
                    if(row.fleet_driver == "" || row.fleet_driver == null || row.fleet_driver == undefined){

                    }else{
                        html+='  司机:'+row.fleet_driver;
                    }
                    if(row.reckoner == "" || row.reckoner == null || row.reckoner == undefined){

                    }else{
                        html+='  结算人:'+row.reckoner+'</p>';
                    }
                    if(row.order_confirm!=''){
                        var confirm = JSON.parse(row.order_confirm);
                        for(var key in confirm){
                            html += '<p class="text-danger">'+confirm[key]+'</p>';
                        }
                    }
                    if(row.order_pay_list.length>0){
                        var rows = row.order_pay_list;
                        for(var i=0;i<rows.length;i++){
                            html += '<p class="text-danger">'+rows[i].pay_title+'</p>';
                        }
                        return html;
                    }else{
                        return html;
                    }
                },
                showExport: true,
                exportDataType: "basic"
            });
        });
        //检索日期
        In('laydate',function () {
            laydate.render({
                elem: '#rangdate',
                range: '~'
            });
        })
    });
    //刷新表格
    function refresh() {
        $('#grid').bootstrapTable('refresh');
    }
    //添加
    function add() {
        var mid="{$Request.param.mid}";
        location.href="__URL__/add.html?mid="+mid;
    }
    //选择司机
    function selectDriver(obj) {
        var aid=$(obj).data('aid');
        top.layer.open({
            title: '选择司机',
            type: 2,
            area: ['1000px', '450px'],
            content: '/system/logic.select/driver.html?aid='+aid,
            btn:["选择","取消"],
            yes:function (index) {
                var tindex=index;
                var drivers = top.layer.getChildFrame('#drivers', tindex).val();
                var driver_ids = top.layer.getChildFrame('#driver_ids', tindex).val();
                $.post('/system/logic.order/setdri.html',{aid:aid,fleet_driver:drivers,fleet_driver_ids:driver_ids},function(res){
                    if(res.code==1){
                        top.layer.close(tindex);
                        refresh();
                    }else{
                        top.layer.alert(res.msg);
                    }
                })
            }
        });
    }
    
    //显示异常
    function showLeave(dri_ids) {
        $.get('/system/logic.query/badleave.html?dri_ids='+dri_ids,function (data) {
            //data
            In('template',function(){
            	var html=template('leave_layer',data);
	            top.layer.open({
	                title: '异常详情',
	                type:1,
	                content: html
	            });
            });
        })
    }
    //去除司机
    function delLeave(aid) {
        var url = "/system/logic.order/delleave.html";
        var params = {'aid': aid, 'mid': "{$Request.param.mid}"};
        doConfirm('确认要清除该行程单对应司机吗？', url, params, function (data) {
            if (data.code == 1) {
                refresh();
            }
        });
    }
    //批量删除文章
    function batchDel(aid) {
        if (IsMore(aid)) {
            var url = "{:url('del')}";
            var params = {'aid': aid, 'mid': "{$Request.param.mid}"};
            doConfirm('你确认要删除吗？', url, params, function (data) {
                if (data.code == 1) {
                    refresh();
                }
            });
        }
    }
    //更新排序
    function upOrder() {
        if ($(".batchSort").length == 0) {
            alert('您还没有添加内容！');
            return false;
        }
        var params = $(".batchSort").serialize();
        var url = "__URL__/uporder.html?mid={$Request.param.mid}";
        getAjax(url, params, function (data) {
            if (data.code == 1) {
                refresh();
            }
        });
    }
    //设置预借款
    function setSjPremoney(aid,sj_pre_money) {
        top.layer.prompt({
            formType: 0,
            value: sj_pre_money,
            title: '请输入司机预借金额'
        }, function(value, index, elem){
            var url = "/system/logic.order/setsjpremoney.html?mid={$Request.param.mid}";
            getAjax(url, {aid:aid,sj_pre_money:value}, function (data) {
                if (data.code == 1) {
                    top.layer.msg('设置成功！',function () {
                        refresh();
                    })
                }
            });
            top.layer.close(index);
        });
    }
    //出车收车
    function setstatus($aid,$status) {
        var url="/system/logic.order/setstatus.html";
        if($status==0){
            var title='请输入出发里程数';
        }else{
            var title='请输入结束里程数';
        }
        top.layer.prompt({
            formType: 0,
            value: 0,
            title: title
        }, function(value, index, elem){
            if($status==0){
                var params={aid:$aid,order_status:$status,order_start_mm:value};
            }else{
                var params={aid:$aid,order_status:$status,order_end_mm:value};
            }
            getAjax(url, params, function (data) {
                if (data.code == 1) {
                    top.layer.msg('设置成功！',function () {
                        refresh();
                    })
                }
            });
            top.layer.close(index);
        });
    }
</script>
{/block}